package logic

import (
	"context"
	"fmt"
	"we-chat-reg/cache"
	"we-chat-reg/model"

	"we-chat-reg/weixinrpc/internal/svc"
	"we-chat-reg/weixinrpc/weixinrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *weixinrpc.RegisterRequest) (*weixinrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	var Weichat model.WeiChat

	model.DB.Debug().Find("tel=?", in.Tel).Find(&Weichat)
	if Weichat.ID != 0 {
		return nil, fmt.Errorf("该账号已注册,请登录")
	}
	s, _ := cache.Rdb.Get(l.ctx, "sendSms"+in.Tel).Result()
	if s != in.SendSms {
		return nil, fmt.Errorf("验证码错误")
	}
	Weichat = model.WeiChat{

		Img:      in.Img,
		Address:  in.Address,
		Tel:      in.Tel,
		Username: in.Username,
		Password: in.Password,
	}
	model.DB.Debug().Create(&Weichat)

	return &weixinrpc.RegisterResponse{
		Id: int64(Weichat.ID),
	}, nil
}
